package com.yuanfeng.action.admin.book.dao;
import com.yuanfeng.pojo.BookType;
import com.yuanfeng.pojo.Books;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class BookDaoImpl extends HibernateDaoSupport implements BookDao {

    /**
     * 保存图书
     * @param book
     */
    public void save(Books book) {

        Books books = new Books();
        books.setId(book.getId());
        books.setName(book.getName());
        books.setPrice(book.getPrice());
        books.setAuth(book.getAuth());
        books.setImg(book.getImg());
        books.setRebate(book.getRebate());
        books.setStock(book.getStock());
        books.setPublisher(book.getPublisher());
        books.setPublishdate(book.getPublishdate());
        books.setPages(book.getSize());
        books.setPrinttimes(book.getPrinttimes());
        books.setVersions(book.getVersions());
        books.setBrief(book.getBrief());
        books.setContent(book.getContent());
        books.setOnlinetime(book.getOnlinetime());
        this.getHibernateTemplate().save(books);
        //必须调用flush()和clear()函数,不然出现jdbc批量插入数据失败
        this.getHibernateTemplate().flush();
        this.getHibernateTemplate().clear();
        //保存图书类型
        BookType bookType = new BookType();

        for (String typeId : book.getTypes()) {
            bookType.setBookId(book.getId());
            bookType.setTypeId(typeId);
            this.getHibernateTemplate().save(bookType);
            this.getHibernateTemplate().flush();
        }

    }

}
